package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.DishFlavorMapper;
import cn.usian.dao.DishMapper;
import cn.usian.dto.DishDto;
import cn.usian.pojo.Dish;
import cn.usian.pojo.DishFlavor;
import cn.usian.pojo.Employee;
import cn.usian.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;


    @Override
    public R findPage(Integer page, Integer pageSize, String name) {
        //创建page对象
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        //进行全查
        List<DishDto> list =  dishMapper.findPage(pageInfo,name);
        pageInfo.setRecords(list);
        return R.success(pageInfo);
    }


    @Override
    public R findById(Long id) {
        //根据id查询dish
        Dish dish = dishMapper.selectById(id);
        //根据dish_id查询对应口味
        List<DishFlavor> list = dishFlavorMapper.findDishId(id);
        //返回的对象dishDto
        DishDto dishDto = new DishDto();
        //将dish对象中的数据复制到dishDto中
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(list);
        return R.success(dishDto);
    }

    @Override
    public void update(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        //修改dish表
        dishMapper.updateById(dishDto);
        LambdaUpdateWrapper<DishFlavor> dishFlavorLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        dishFlavorLambdaUpdateWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        //修改dish_flavor
        dishFlavorMapper.delete(dishFlavorLambdaUpdateWrapper);
        //从dishdto对象中取出所有flavor对象
        List<DishFlavor> flavors = dishDto.getFlavors();
        //将所有的list中的dishflavor对象保存起来
        for (DishFlavor df: flavors) {
            dishFlavorMapper.insert(df);
        }
    }

    @Override
        public R add(DishDto dishDto) {
            //先添加dish菜品
            dishDto.setCreateTime(LocalDateTime.now());
            dishDto.setUpdateTime(LocalDateTime.now());
            dishDto.setSort(1);
            dishMapper.insert(dishDto);
            //添加口味
            List<DishFlavor> flavors = dishDto.getFlavors();
            //将手动需要赋值的参数给添加到DishFlavor对象中
            for (DishFlavor flavor : flavors) {
                flavor.setCreateTime(LocalDateTime.now());
                flavor.setUpdateTime(LocalDateTime.now());
                flavor.setCreateUser(dishDto.getCreateUser());
                flavor.setUpdateUser(dishDto.getUpdateUser());
                flavor.setDishId(dishDto.getId());
                flavor.setIsDeleted(0);
            }
            //批量插入菜品口味
            for (DishFlavor df : flavors) {
                dishFlavorMapper.insert(df);
            }
            return R.success("添加成功");
        }
    }

